﻿using NUnit.Framework;
using Sasa.QualityTools.CodeAnalysis.Core.Models;
using System;
using System.IO;
using System.Linq;

namespace Sasa.QualityTools.CodeAnalysis.Core.Tests.Unit.Models
{
    [TestFixture]
    public class FxCopDictionaryTest
    {
        [Test]
        public void TestLoad()
        {
            var contents = new string[] {
                @"<?xml version=""1.0""?>",
                @"<Dictionary>",
                @"    <Words>",
                @"        <Recognized>",
                @"            <Word>a</Word>",
                @"        </Recognized>",
                @"        <Unrecognized>",
                @"            <Word>b</Word>",
                @"        </Unrecognized>",
                @"        <Deprecated>",
                @"            <Term PreferredAlternate=""d"">c</Term>",
                @"        </Deprecated>",
                @"        <Compound>",
                @"            <Term CompoundAlternate=""f"">e</Term>",
                @"        </Compound>",
                @"        <DiscreteExceptions>",
                @"            <Term>g</Term>",
                @"        </DiscreteExceptions>",
                @"    </Words>",
                @"    <Acronyms>",
                @"        <CasingExceptions>",
                @"            <Acronym>h</Acronym>",
                @"        </CasingExceptions>",
                @"    </Acronyms>",
                @"</Dictionary>" };
            var path = GetType().Name + ".xml";
            File.WriteAllLines(path, contents);

            var testee = new FxCopDictionary(path);
            CollectionAssert.AreEqual(new string[] { "a" }, testee.Words.Recognized);
            CollectionAssert.AreEqual(new string[] { "b" }, testee.Words.Unrecognized);
            CollectionAssert.AreEqual(new string[] { "c,d" },
                testee.Words.Deprecated.Select(o => o.Value + "," + o.Alternate));
            CollectionAssert.AreEqual(new string[] { "e,f" },
                testee.Words.Compound.Select(o => o.Value + "," + o.Alternate));
            CollectionAssert.AreEqual(new string[] { "g" }, testee.Words.DiscreteExceptions);
            CollectionAssert.AreEqual(new string[] { "h" }, testee.Acronyms.CasingExceptions);
        }

        [Test]
        public void TestSave()
        {
            var path = Path.GetTempFileName();
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            var testee = new FxCopDictionary(path);
            testee.Words.Recognized.Add("a");
            testee.Words.Unrecognized.Add("b");
            testee.Words.Deprecated.Add(new DeprecatedTerm { Value = "c", Alternate = "d" });
            testee.Words.Compound.Add(new CompoundTerm { Value = "e", Alternate = "f" });
            testee.Words.DiscreteExceptions.Add("g");
            testee.Acronyms.CasingExceptions.Add("h");
            testee.Save();

            var actual = File.ReadAllLines(path);
            var expected = new string[] {
                @"<?xml version=""1.0""?>",
                @"<Dictionary>",
                @"    <Words>",
                @"        <Recognized>",
                @"            <Word>a</Word>",
                @"        </Recognized>",
                @"        <Unrecognized>",
                @"            <Word>b</Word>",
                @"        </Unrecognized>",
                @"        <Deprecated>",
                @"            <Term PreferredAlternate=""d"">c</Term>",
                @"        </Deprecated>",
                @"        <Compound>",
                @"            <Term CompoundAlternate=""f"">e</Term>",
                @"        </Compound>",
                @"        <DiscreteExceptions>",
                @"            <Term>g</Term>",
                @"        </DiscreteExceptions>",
                @"    </Words>",
                @"    <Acronyms>",
                @"        <CasingExceptions>",
                @"            <Acronym>h</Acronym>",
                @"        </CasingExceptions>",
                @"    </Acronyms>",
                @"</Dictionary>" };

            CollectionAssert.AreEqual(expected.Select(o => o.Trim()).ToList(), actual.Select(o => o.Trim()).ToList());

            File.Delete(path);
        }
    }
}
